package com.dbsop.domain;

import com.dbsop.EsClient;
import com.dbsop.domain.agg.AggOperation;
import com.dbsop.domain.agg.Group;
import com.dbsop.domain.rest.EsAggRestCondition;
import com.dbsop.domain.transport.EsAggTcCondition;
import com.dbsop.domain.transport.EsTransportCondition;
import com.dbsop.domain.rest.info.RFFM;

import java.util.ArrayList;
import java.util.List;

/**
 * Es 聚合操作
 */
public abstract class EsAggCondition {

    protected Group group;

    protected List<AggOperation> aggOperations = new ArrayList<AggOperation>();

    protected EsCondition esCondition;

    protected int recordSize = 0;

    public static EsAggCondition getInstance(){
        boolean useHttp = EsClient.getEsClient().isUseHttp();
        if (!useHttp){
            return new EsAggTcCondition();
        }
        return new EsAggRestCondition();
    }

    public Group getGroup() {
        return group;
    }

    public int getRecordSize() {
        return recordSize;
    }

    public List<AggOperation> getAggOperations() {
        return aggOperations;
    }

    public EsCondition getEsCondition() {
        if (this.esCondition == null) esCondition = EsTransportCondition.getEsCondition();
        return esCondition;
    }

    public EsAggCondition set(EsCondition esCondition){
        this.esCondition = esCondition;
        return this;
    }

    public EsCondition getCondition(){
        if (this.esCondition == null) esCondition = EsTransportCondition.getEsCondition();
        return esCondition;
    }

    public void add(AggOperation aggOperation){
        aggOperations.add(aggOperation);
    }

    public abstract Group Group(String fieldName);

    public abstract Group Group(String fieldName, String formatName);

    public abstract EsAggCondition group(String fieldName);

    public abstract EsAggCondition group(String fieldName, String formatName);


    public abstract Group RangeGroup(String fieldName, RFFM... rffms);

    public  abstract Group RangeGroup(String fieldName, String formatName, RFFM ... rffms);

    public abstract EsAggCondition rangeGroup(String fieldName, RFFM... rffms);

    public abstract EsAggCondition rangeGroup(String fieldName, String formatName, RFFM... rffms);


    public abstract EsAggCondition Sum(String fieldName);

    public abstract EsAggCondition Avg(String fieldName);

    public abstract EsAggCondition Count(String fieldName);

    public abstract EsAggCondition Max(String fieldName, String... fields);


    public abstract EsAggCondition MaxExtFields(String fieldName, String... fields);

    public abstract EsAggCondition MaxExtFields(String fieldName, int num, String... fields);


    public abstract EsAggCondition Min(String fieldName, String... fields);

    public abstract EsAggCondition MinExtFields(String fieldName, String... fields);

    public abstract EsAggCondition MinExtFields(String fieldName, int num, String... fields);



    public abstract EsAggCondition Sum(String fieldName, String formatName);

    public abstract EsAggCondition Avg(String fieldName, String formatName);

    public abstract EsAggCondition Count(String fieldName, String formatName);

    public abstract EsAggCondition Max(String fieldName, String formatName);

    public abstract EsAggCondition Min(String fieldName, String formatName);

    public void recordSize(int size){
        recordSize = size;
    }


}
